package com.jf.StudenManage.service;

import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import com.jf.StudenManage.dao.StudentDao;
import com.jf.StudenManage.pojo.Student;

public class StudentService {
/*
    用户实现登陆功能  用户 在    用户名输入框中可以输入  用户名或者是  手机号码  
     用户名+密码可以进行登陆
    手机号码+密码可以进行登陆
*/
	  private  StudentDao  studentDao=new  StudentDao();
	
	public   void  register(Student  student){
		String   insertSql="insert  into  student (name,password,sno,gender,telephone) values ('"+student.getName()+"','"+student.getPassword()+"','"+student.getSno()+"','"+student.getGender()+"','"+student.getTelephone()+"')";
	    studentDao.insertStudent(insertSql);
	} 
	  
	public   Student   loadByName(String  username){
		return  studentDao.queryStudentByName(username);
	}
	
	public    Student   loadByTelephone(String  telephone){
		return   studentDao.queryStudentByTelephone(telephone);
	}
	public   String  login(String  content,String password ,HttpServletResponse  response){
		 String  result="";
		 Student  student=loadByName(content);
		 if(student==null){//压根没有这个用户名存在
			 result="用户不存在";
			 student=loadByTelephone(content);
			 if(student==null){
			 }else{
				 if(password.equals(student.getPassword())){
					 result="使用手机号码+密码登录成功";
				 }else{
					 result="密码错误";
				 }
			 }
		 }else{
			 if(password.equals(student.getPassword())){
				 result="使用用户名+密码登录成功";
			 }else{
				 result="密码错误";
			 }
		 }
//		 
//	 switch (result) {
//		case "使用用户名+密码登录成功":
//			response.setStatus(Resource.LOGIN_SUCCESS);
//			break;
//		case   "密码错误":
//			response.setStatus(Resource.PASSWORD_WRONG);
//		default:
//			response.setStatus(Resource.USER_NOTFOND);
//			break;
//		}
		return   result;
	}
	
//	public   ArrayList<Student>  backAllStudents(){
//           		
//	   return   studentDao.queryAll();
//	}
	
	public  Object[]   backAllStudentsByPaginer(String pageNum){
		Object[]  obj=new Object[2];
		int counts= studentDao.backCounts();
		 
		if(counts%3==0){
			obj[0]=counts/3;
		}else{
			obj[0]=counts/3+1;
		}
	    //分页查询返回 studentlist
		obj[1]=studentDao.queryByPagion(pageNum);
		return  obj;
	}
	
	
	
	
	public   void  delteById(String sid){
		studentDao.delete(sid);
	}
	
	public   Object[]   deleStudentById(String  sid){
		//根据id删除学生的操作
		delteById(sid);
		return  backAllStudentsByPaginer("1");
	}
	
	public   Student    backStudentById(String  sid){
		return   studentDao.queryStudentBySid(sid);
	}
	
	public   void   updateStudent(Student s){
		studentDao.update(s);
	}
	public  Object[]  deleteSomeByIds(String sids){
		String[]  data=sids.split(",");
		for (int i = 0; i < data.length; i++) {
			delteById(data[i]);
		}
		return  backAllStudentsByPaginer("1");
	}
	
	public   Object[]    updateStudentById(Student  student){
		updateStudent(student);
		return backAllStudentsByPaginer("1");
	}
	
	
	
	
}
